package iscript.uiFramework.components.form 
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import iscript.uiFramework.components.InteractiveUIComponent;
	import iscript.uiFramework.components.spacer.BaseSpacer;
	import iscript.uiFramework.components.text.Label;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.event.RadioEvent;

	import iscript.uiFramework.event.UIComponentEvent;
	import iscript.uiFramework.UIFramework;
	
	
	public class Radio extends InteractiveUIComponent
	{
		private var _spacer:BaseSpacer;
		private var _label:Label;
		private var _radioNormal:BitmapData;
		private var _radioHover:BitmapData;
		private var _radioSelected:BitmapData;
		private var _selected:Boolean;
		private var _text:String;
		private var _value:Object;
 
		
		override protected function initialize():void 
		{
			_radioNormal =  UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".Radio.Normal");
			_radioHover =   UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".Radio.Hover");
			_radioSelected =  UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".Radio.Selected");			
			_spacer = new BaseSpacer(true)
			_spacer.render(this);			
			_label = new Label();
			_label.text = "";
			_label.icon = _radioNormal;
			_label.addEventListener("updateview",_labelUpdate)
			_label.render(this);
			
			_spacer.width = _label.width;
			_spacer.height = _label.height;
			
			super.initialize();
		}
		
		override protected function eventListener():void 
		{
			ClickHandler = _selectHandlerLogic;
			HoverHandler = _hoverHandlerLogic;
			OutHandler = _outHandlerLogic;
			super.eventListener();
		}
			
		private function _outHandlerLogic(e:*):void 
		{
			if (!_selected) {
				_label.icon = _radioNormal;
				}
		}
		
		private function _hoverHandlerLogic(e:*):void 
		{
			if (!_selected) {
				_label.icon = _radioHover;
				}
		}
		
		private function _selectHandlerLogic(e:*):void 
		{
			if(!_selected){
				selected = true;
				var event:RadioEvent = new RadioEvent(RadioEvent.Select);
					event.selected = _selected;
					event.text = _label.text;
					event.value = _value;
				this.dispatchEvent(event);
			}
		}
		
		
		private function _labelUpdate(e:*):void 
		{
			_spacer.width = _label.width;
			_spacer.height = _label.height;
		}
		
		public function get selected():Boolean 
		{
			return _selected;
		}
		
		public function set selected(value:Boolean):void 
		{
			_selected = value;
			if (value) {
				_label.icon = _radioSelected;
				}else {
				_label.icon = _radioNormal;
				}
			
		}
		
		public function get text():String 
		{
			return _text;
		}
		
		public function set text(value:String):void 
		{
			_text = value;
			_label.text = value;
		}
		
		public function get value():Object 
		{
			return _value;
		}
		
		public function set value(value:Object):void 
		{
			_value = value;
		}
		
	}
}